Attribute VB_Name = "windrvr"

'  W i n D r i v e r    v 5 . 0 0
'
'  Header file for Windows 95/98/ME/NT/NTE/NT2000.
'  This file may not be distributed, it is only for evaluation & development purposes.
'
'  Web site as http://www.jungo.com/windrv
'  Email as      support@jungo.com
'
'  (C) Jungo 2001

Option Explicit

Global Const WD_VER = 500

Global Const WD_VER_STR = "WinDriver V5.00 Jungo (c) 2001"
Global Const WD_VER_MODULE = WD_VER_STR

Global Const CMD_NONE = 0         ' No command
Global Const CMD_END = 1          ' End command

Global Const RP_BYTE = 10         ' Read port byte
Global Const RP_WORD = 11         ' Read port word
Global Const RP_DWORD = 12        ' Read port dword
Global Const WP_BYTE = 13         ' Write port byte
Global Const WP_WORD = 14         ' Write port word
Global Const WP_DWORD = 15        ' Write port dword

Global Const RP_SBYTE = 20        ' Read port string byte
Global Const RP_SWORD = 21        ' Read port string word
Global Const RP_SDWORD = 22       ' Read port string dword
Global Const WP_SBYTE = 23        ' Write port string byte
Global Const WP_SWORD = 24        ' Write port string word
Global Const WP_SDWORD = 25       ' Write port string dword

Global Const RM_BYTE = 30         ' Read memory byte
Global Const RM_WORD = 31         ' Read memory word
Global Const RM_DWORD = 32        ' Read memory dword
Global Const WM_BYTE = 33         ' Write memory byte
Global Const WM_WORD = 34         ' Write memory word
Global Const WM_DWORD = 35        ' Write memory dword

Global Const RM_SBYTE = 40        ' Read memory string byte
Global Const RM_SWORD = 41        ' Read memory string word
Global Const RM_SDWORD = 42       ' Read memory string dword
Global Const WM_SBYTE = 43        ' Write memory string byte
Global Const WM_SWORD = 44        ' Write memory string word
Global Const WM_SDWORD = 45       ' Write memory string dword

Global Const WM_APP As Long = &H8000&    ' Platform SDK constant

Global Const WD_DMA_PAGES = 256

Global Const DMA_KERNEL_BUFFER_ALLOC = 1 ' the system allocates a contiguous buffer
                                         ' the user doesnt need to supply linear_address
Global Const DMA_KBUF_BELOW_16M = 2      ' if DMA_KERNEL_BUFFER_ALLOC if used,
                                         ' this will make sure it is under 16M
Global Const DMA_LARGE_BUFFER = 4        ' if DMA_LARGE_BUFFER if used,
                                 ' the maximum number of pages are dwPages, and not
                                 ' WD_DMA_PAGES. if you lock a user buffer (not a kernel
                                 ' allocated buffer) that is larger than 1MB, then use this
                                 ' option, and allocate memory for pages.


Type PVOID
    ptr As Long
End Type
   
 

Type WD_DMA_PAGE
    pPhysicalAddr As PVOID        ' physical address of page
    dwBytes       As Long        ' size of page
End Type


Type WD_DMA
    hDMA       As Long      ' handle of DMA buffer
    pUserAddr  As PVOID      ' beginning of buffer
    dwBytes    As Long      ' size of buffer
    dwOptions  As Long      ' allocation options
    dwPages    As Long      ' number of pages in buffer
    Page(0 To WD_DMA_PAGES - 1) As WD_DMA_PAGE
End Type

Type WD_TRANSFER
    cmdTrans    As Long  ' Transfer command WD_TRANSFER_CMD
    dwPort      As Long  ' IO port for transfer or user memory address
    dwBytes     As Long  ' For string transfer
    fAutoInc    As Long  ' Transfer from one port/address or
                         ' use incremental range of addresses
    dwOptions   As Long  ' Must be 0
    dwDataTransfer As Long
End Type

Type PWD_TRANSFER    ' pointer to WD_TRANSFER
    cmd As Long
End Type

Private IOCTL_WD_DMA_LOCK As Long
Private IOCTL_WD_DMA_UNLOCK As Long
Private IOCTL_WD_TRANSFER As Long
Private IOCTL_WD_MULTI_TRANSFER As Long
Private IOCTL_WD_PCI_SCAN_CARDS As Long
Private IOCTL_WD_PCI_GET_CARD_INFO As Long
Private IOCTL_WD_VERSION As Long
Private IOCTL_WD_LICENSE As Long
Private IOCTL_WD_PCI_CONFIG_DUMP As Long
Private IOCTL_WD_KERNEL_PLUGIN_OPEN As Long
Private IOCTL_WD_KERNEL_PLUGIN_CLOSE As Long
Private IOCTL_WD_KERNEL_PLUGIN_CALL As Long
Private IOCTL_WD_INT_ENABLE As Long
Private IOCTL_WD_INT_DISABLE As Long
Private IOCTL_WD_INT_COUNT As Long
Private IOCTL_WD_INT_WAIT As Long
Private IOCTL_WD_ISAPNP_SCAN_CARDS As Long
Private IOCTL_WD_ISAPNP_GET_CARD_INFO As Long
Private IOCTL_WD_ISAPNP_CONFIG_DUMP As Long
Private IOCTL_WD_PCMCIA_SCAN_CARDS As Long
Private IOCTL_WD_PCMCIA_GET_CARD_INFO As Long
Private IOCTL_WD_PCMCIA_CONFIG_DUMP As Long
Private IOCTL_WD_SLEEP As Long
Private IOCTL_WD_DEBUG As Long
Private IOCTL_WD_DEBUG_DUMP As Long
Private IOCTL_WD_CARD_UNREGISTER As Long
Private IOCTL_WD_CARD_REGISTER As Long


Global Const INTERRUPT_LEVEL_SENSITIVE = 1
Global Const INTERRUPT_CMD_COPY = 2

Type WD_KERNEL_PLUGIN_CALL
    hKernelPlugIn As Long
    dwMessage     As Long
    pData         As PVOID
    dwResult      As Long
End Type

Type WD_INTERRUPT
    hInterrupt As Long ' handle of interrupt
    dwOptions  As Long ' interrupt options as INTERRUPT_CMD_COPY
    cmd        As PWD_TRANSFER ' commands to do on interrupt
    dwCmds     As Long         ' number of commands for WD_IntEnable()
    kpCall    As WD_KERNEL_PLUGIN_CALL ' kernel plugin call
    fEnableOk As Long     ' did WD_IntEnable() succeed
                          ' For WD_IntWait() and WD_IntCount()
    dwCounter As Long ' number of interrupts received
    dwLost    As Long ' number of interrupts not yet dealt with
    fStopped  As Long ' was interrupt disabled during wait
End Type

Type WD_VERSION
    dwVer As Long
    cVer(0 To 100 - 1) As Byte ' needs testing if pascal string is compatible
End Type

Global Const LICENSE_DEMO As Long = &H1
Global Const LICENSE_WD   As Long = &H4
Global Const LICENSE_KD   As Long = &H400000
Global Const LICENSE_IO   As Long = &H8
Global Const LICENSE_MEM  As Long = &H10
Global Const LICENSE_INT  As Long = &H20
Global Const LICENSE_PCI  As Long = &H40
Global Const LICENSE_DMA  As Long = &H80
Global Const LICENSE_NT   As Long = &H100
Global Const LICENSE_95   As Long = &H200
Global Const LICENSE_ISAPNP   As Long = &H400
Global Const LICENSE_PCMCIA   As Long = &H800
Global Const LICENSE_PCI_DUMP As Long = &H1000
Global Const LICENSE_MSG_GEN  As Long = &H2000
Global Const LICENSE_MSG_EDU  As Long = &H4000
Global Const LICENSE_MSG_INT  As Long = &H8000&
Global Const LICENSE_KER_PLUG As Long = &H10000
Global Const LICENSE_LINUX As Long = &H20000
Global Const LICENSE_CE    As Long = &H80000
Global Const LICENSE_VXWORKS As Long = &H10000000
Global Const LICENSE_THIS_PC As Long = &H100000
Global Const LICENSE_WIZARD  As Long = &H200000
Global Const LICENSE_SOLARIS As Long = &H800000
Global Const LICENSE_CPU0 As Long = &H40000
Global Const LICENSE_CPU1 As Long = &H1000000
Global Const LICENSE_CPU2 As Long = &H2000000
Global Const LICENSE_CPU3 As Long = &H4000000
Global Const LICENSE_USB  As Long = &H8000000

Global Const LICENSE2_CPCI As Long = &H1
Global Const LICENSE2_REMOTE As Long = &H2

Type WD_LICENSE
    cLicense(0 To 100 - 1) As Byte ' buffer with license string to
                            ' put. If empty string then get
                            ' current license setting into
                            ' dwLicense
    dwLicense  As Long  ' Returns license settings:
    dwLicense2 As Long  ' LICENSE_DEMO, LICENSE_WD etc...
                            ' If put license was unsuccessful
                            ' (i.e. invalid license) then dwLicense
                            ' will return 0.
End Type

Type WD_BUS
    dwBusType  As Long      ' Bus Type: ISA, EISA, PCI, PCMCIA
    dwBusNum   As Long      ' Bus number
    dwSlotFunc As Long      ' Slot number on Bus
End Type

Global Const WD_BUS_ISA = 1
Global Const WD_BUS_EISA = 2
Global Const WD_BUS_PCI = 5
Global Const WD_BUS_PCMCIA = 8

Global Const ITEM_NONE = 0
Global Const ITEM_INTERRUPT = 1
Global Const ITEM_MEMORY = 2
Global Const ITEM_IO = 3
Global Const ITEM_BUS = 5

Type WD_ITEMS
    Item         As Long ' ITEM_TYPE
    fNotSharable As Long
    ' for ITEM_INTERRUPT
    ' dw1 - number of interrupt to install
    ' dw2 - interrupt options: INTERRUPT_LEVEL_SENSITIVE
    ' dw3 - returns the handle of the interrupt installed
    ' for ITEM_MEMORY
    ' dw1 - physical address on card
    ' dw2 - address range
    ' dw3 - returns the address to pass on to transfer commands
    ' dw4 - returns the address for direct user read/write
    ' dw5 - returns the CPU physical address of card
    ' for ITEM_IO
    ' dw1 - beginning of io address
    ' dw2 - io range
    dw1 As Long
    dw2 As Long
    dw3 As Long
    dw4 As Long
    dw5 As Long
End Type

Global Const WD_CARD_ITEMS = 20

Type WD_CARD
    dwItems As Long
    Item(0 To WD_CARD_ITEMS - 1) As WD_ITEMS
End Type

Type WD_CARD_REGISTER
    Card           As WD_CARD ' card to register
    fCheckLockOnly As Long    ' only check if card is lockable, return hCard=1 if OK
    hCard          As Long    ' handle of card
    dwOptions      As Long    ' should be zero
    cName(0 To 31)      As Byte        ' name of card
    cDescription(0 To 100 - 1) As Byte ' description
End Type

Global Const WD_PCI_CARDS = 30

Type WD_PCI_SLOT
    dwBus  As Long
    dwSlot As Long
    dwFunction As Long
End Type

Type WD_PCI_ID
    dwVendorId As Long
    dwDeviceId As Long
End Type

Type WD_PCI_SCAN_CARDS
    searchId As WD_PCI_ID ' if dwVendorId = 0, scan all
                          ' vendor Ids
                          ' if dwDeviceId = 0, scan all
                          ' device Ids
    dwCards  As Long   ' Number of cards found
    cardId(0 To WD_PCI_CARDS - 1) As WD_PCI_ID
                     ' VendorID & DeviceID of cards found
    cardSlot(0 To WD_PCI_CARDS - 1) As WD_PCI_SLOT
                     ' PCI slot info of cards found
End Type

Type WD_PCI_CARD_INFO
    pciSlot As WD_PCI_SLOT ' PCI slot
    Card    As WD_CARD     ' Get card parameters for PCI slot
End Type

Global Const PCI_ACCESS_OK = 0
Global Const PCI_ACCESS_ERROR = 1
Global Const PCI_BAD_BUS = 2
Global Const PCI_BAD_SLOT = 3

Type WD_PCI_CONFIG_DUMP
    pciSlot  As WD_PCI_SLOT ' PCI bus, slot and function number
    pBuffer  As PVOID       ' buffer for read/write
    dwOffset As Long        ' offset in pci configuration space
                            ' to read/write from
    dwBytes  As Long       ' bytes to read/write from/to buffer
                                ' returns the # of bytes read/written
    fIsRead  As Long       ' if 1 then read pci config, 0 write pci config
    dwResult As Long       ' PCI_ACCESS_RESULT
End Type

Global Const WD_ISAPNP_CARDS = 16
Global Const WD_ISAPNP_COMPATIBLE_IDS = 10
Global Const WD_ISAPNP_COMP_ID_LENGTH = 7  ' ISA compressed ID is 7 chars long
Global Const WD_ISAPNP_ANSI_LENGTH = 32    ' ISA ANSI ID is limited to 32 chars long


Type WD_ISAPNP_COMP_ID
    compID(0 To WD_ISAPNP_COMP_ID_LENGTH) As Byte
End Type
Type WD_ISAPNP_ANSI
    ansi(0 To WD_ISAPNP_ANSI_LENGTH + 3) As Byte ' add 3 bytes for DWORD alignment
End Type



Type WD_ISAPNP_CARD_ID
    cVendor  As WD_ISAPNP_COMP_ID ' Vendor ID
    dwSerial As Long ' Serial number of card
End Type


Type WD_ISAPNP_CARD
    cardId As WD_ISAPNP_CARD_ID ' VendorID & serial number of cards found
    dcLogicalDevices As Long   ' Logical devices on the card
    bPnPVersionMajor As Byte    ' ISA PnP version Major
    bPnPVersionMinor As Byte    ' ISA PnP version Minor
    bVendorVersionMajor As Byte ' Vendor version Major
    bVendorVersionMinor As Byte ' Vendor version Minor
    cIdent As WD_ISAPNP_ANSI    ' Device identifier
End Type

Type WD_ISAPNP_SCAN_CARDS
    searchId As WD_ISAPNP_CARD_ID ' if searchId.cVendor[0]==0 - scan all vendor IDs
                                      ' if searchId.dwSerial==0 - scan all serial numbers
    dwCards As Long  ' number of cards found
    Card(0 To WD_ISAPNP_CARDS - 1) As WD_ISAPNP_CARD ' cards found
End Type


Type WD_ISAPNP_CARD_INFO
    cardId   As WD_ISAPNP_CARD_ID  ' VendorID and serial number of card
    dwLogicalDevice     As Long   ' logical device in card
    cLogicalDeviceId    As WD_ISAPNP_COMP_ID  ' logical device ID
    dwCompatibleDevices As Long   ' number of compatible device IDs
    CompatibleDevice(0 To WD_ISAPNP_COMPATIBLE_IDS - 1) As WD_ISAPNP_COMP_ID ' Compatible device IDs
    cIdent As WD_ISAPNP_ANSI  ' Device identifier
    Card As WD_CARD  ' get card parameters for the ISA PnP card
End Type

Global Const ISAPNP_ACCESS_OK = 0
Global Const ISAPNP_ACCESS_ERROR = 1
Global Const ISAPNP_BAD_ID = 2

Type WD_ISAPNP_CONFIG_DUMP
    cardId   As WD_ISAPNP_CARD_ID  ' VendorID and serial number of card
    dwOffset As Long   ' offset in ISA PnP configuration space to read/write from
    fIsRead  As Long   ' if 1 then read ISA PnP config, 0 write ISA PnP config
    bData    As Byte    ' result data of byte read/write
    dwResult As Long ' ISAPNP_ACCESS_RESULT
End Type

Global Const WD_PCMCIA_CARDS = 8
Global Const WD_PCMCIA_VERSION_LEN = 4
Global Const WD_PCMCIA_MANUFACTURER_LEN = 48
Global Const WD_PCMCIA_PRODUCTNAME_LEN = 48
Global Const WD_PCMCIA_MAX_SOCKET = 2
Global Const WD_PCMCIA_MAX_FUNCTION = 2


Type WD_PCMCIA_SLOT
    uSocket    As Byte  ' Specifies the socket number (first socket is 0)
    uFunction  As Byte  ' Specifies the function number (first function is 0)
    uPadding0  As Byte  ' 2 bytes padding so structure will be 4 bytes aligned
    uPadding1  As Byte
End Type

Type WD_PCMCIA_ID
    dwManufacturerId As Long ' card manufacturer
    dwCardId         As Long ' card type and model
End Type

Type WD_PCMCIA_SCAN_CARDS
    searchId As WD_PCMCIA_ID                              ' device ID to search for
    dwCards As Long                                       ' number of cards found
    cardId(0 To WD_PCMCIA_CARDS - 1) As WD_PCMCIA_ID      ' device IDs of cards found
    cardSlot(0 To WD_PCMCIA_CARDS - 1) As WD_PCMCIA_SLOT  ' pcmcia slot info of cards found
End Type

Type WD_PCMCIA_CARD_INFO
    pcmciaSlot As WD_PCMCIA_SLOT  ' pcmcia slot
    Card As WD_CARD               ' get card parameters for pcmcia slot
    cVersion(0 To WD_PCMCIA_VERSION_LEN - 1) As Byte
    cManufacturer(0 To WD_PCMCIA_MANUFACTURER_LEN - 1) As Byte
    cProductName(0 To WD_PCMCIA_PRODUCTNAME_LEN - 1) As Byte
    dwManufacturerId As Long      ' card manufacturer
    dwCardId As Long              ' card type and model
    dwFuncId As Long              ' card function code
End Type

Type WD_PCMCIA_CONFIG_DUMP
    pcmciaSlot As WD_PCMCIA_SLOT
    pBuffer    As PVOID     ' buffer for read/write
    dwOffset   As Long      ' offset in pcmcia configuration space to
                            ' read/write from
    dwBytes    As Long      ' bytes to read/write from/to buffer
                            ' returns the number of bytes read/wrote
    fIsRead    As Long      ' if 1 then read pci config, 0 write pci config
    dwResult   As Long      ' PCMCIA_ACCESS_RESULT
End Type

Global Const SLEEP_NON_BUSY = 1

Type WD_SLEEP
    dwMicroSeconds As Long ' Sleep time in Micro Seconds (1/1,000,000 Second)
    dwOptions      As Long ' can be:
                           ' SLEEP_NON_BUSY this is accurate only for times above 10000 uSec
End Type

Global Const D_OFF = 0
Global Const D_ERROR = 1
Global Const D_WARN = 2
Global Const D_INFO = 3
Global Const D_TRACE = 4

Global Const S_ALL As Long = &HFFFFFFFF
Global Const S_IO As Long = &H8
Global Const S_MEM As Long = &H10
Global Const S_INT As Long = &H20
Global Const S_PCI As Long = &H40
Global Const S_DMA As Long = &H80
Global Const S_MISC As Long = &H100
Global Const S_LICENSE As Long = &H200
Global Const S_ISAPNP As Long = &H400
Global Const S_PCMCIA As Long = &H800
Global Const S_KER_PLUG As Long = &H10000
Global Const S_CARD_REG As Long = &H2000

Global Const DEBUG_STATUS = 1
Global Const DEBUG_SET_FILTER = 2
Global Const DEBUG_SET_BUFFER = 3
Global Const DEBUG_CLEAR_BUFFER = 4
Global Const DEBUG_DUMP_SEC_ON = 5
Global Const DEBUG_DUMP_SEC_OFF = 6

'from win32API
Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Declare Function DeviceIoControl Lib "kernel32" ( _
    ByVal hDevice As Long, _
    ByVal dwIoControlCode As Long, _
          lpInBuffer As Any, _
    ByVal nInBufferSize As Long, _
    ByVal lpOutBuffer As Long, _
    ByVal nOutBufferSize As Long, _
          lpBytesReturned As Long, _
    ByVal lpOverlapped As Long) As Long
Private Const GENERIC_READ = &H80000000
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
Private Const OPEN_EXISTING = 3
Private Const FILE_FLAG_OVERLAPPED = &H40000000
Global Const INVALID_HANDLE_VALUE = -1

'call this function with the variable of which you need the pointer and the last two params pass 1
'address = WD_VB_GetAddress (Variable, 1, 1) 'this extracts the address of Variable
Declare Function WD_VB_GetAddress Lib "kernel32" Alias "MulDiv" (ByRef dwNum As Any, ByVal one As Long, ByVal one_ As Long) As Long

'Use external DLL for interrupt handling
Declare Function InterruptThreadEnable Lib "WD_VB.DLL" _
    Alias "WD_VB_InterruptThreadEnable" ( _
    ByRef phThread As Long, _
    ByVal hWD As Long, _
    ByRef pInt As Any, _
    ByVal IntHandler As Long, _
    ByVal pData As Long, _
    ByVal hWnd As Long) As Long
Declare Sub InterruptThreadDisable Lib "WD_VB.DLL" _
    Alias "WD_VB_InterruptThreadDisable" (ByVal hThread As Long)
  
Type WD_DEBUG
    dwCmd As Long          ' DEBUG_COMMAND as DEBUG_STATUS, DEBUG_SET_FILTER, DEBUG_SET_BUFFER, DEBUG_CLEAR_BUFFER
                           ' used for DEBUG_SET_FILTER
    dwLevel As Long        ' DEBUG_LEVEL as D_ERROR, D_WARN..., or D_OFF to turn debugging off
    dwSection As Long      ' DEBUG_SECTION as for all sections in driver as S_ALL
                           ' for partial sections as S_IO, S_MEM...
    dwLevelMessageBox As Long  ' DEBUG_LEVEL to print in a message box
                               ' used for DEBUG_SET_BUFFER
    dwBufferSize As Long   ' size of buffer in kernel
End Type

Type PChar
    charPtr As Long
End Type

Type WD_DEBUG_DUMP
    pcBuffer As PChar ' buffer to receive debug messages
    dwSize As Long    ' size of buffer in bytes
End Type

Type WD_KERNEL_PLUGIN
    hKernelPlugIn As Long
    pcDriverName As PChar
    pcDriverPath As PChar ' if NULL the driver will be searched in the windows system directory
    pOpenData As PVOID
End Type

Global Const PIPE_TYPE_CONTROL = 0
Global Const PIPE_TYPE_ISOCHRONOUS = 1
Global Const PIPE_TYPE_BULK = 2
Global Const PIPE_TYPE_INTERRUPT = 3


Global Const WD_USB_MAX_PIPE_NUMBER        = 32
Global Const WD_USB_MAX_ENDPOINTS          = 32
Global Const WD_USB_MAX_INTERFACES         = 30
Global Const WD_USB_MAX_DEVICE_NUMBER      = 127

Global Const USB_DIR_IN = 1
Global Const USB_DIR_OUT = 2
Global Const USB_DIR_IN_OUT = 3

Type WD_USB_ID
    dwVendorId  As Long
    dwProductId As Long
End Type

Type WD_USB_PIPE_INFO
    dwNumber As Long        ' Pipe 0 is the default pipe
    dwMaximumPacketSize As Long
    type As Long            ' USB_PIPE_TYPE
    direction As Long       ' USB_DIR
                            ' Isochronous, Bulk, Interrupt are either USB_DIR_IN or USB_DIR_OUT
                            ' Control are USB_DIR_IN_OUT
    dwInterval As Long      ' interval in ms relevant to Interrupt pipes
End Type

Type WD_USB_CONFIG_DESC
    dwNumInterfaces As Long
    dwValue         As Long
    dwAttributes    As Long
    MaxPower        As Long
End Type

Type WD_USB_INTERFACE_DESC
    dwNumber           As Long
    dwAlternateSetting As Long
    dwNumEndpoints     As Long
    dwClass            As Long
    dwSubClass         As Long
    dwProtocol         As Long
    dwIndex            As Long
End Type

Type WD_USB_ENDPOINT_DESC
    dwEndpointAddress As Long
    dwAttributes      As Long
    dwMaxPacketSize   As Long
    dwInterval        As Long
End Type

Type WD_USB_INTERFACE
    Interface As WD_USB_INTERFACE_DESC
    Endpoints(0 To WD_USB_MAX_ENDPOINTS - 1) As WD_USB_ENDPOINT_DESC
End Type

Type WD_USB_CONFIGURATION
    uniqueId As Long
    dwConfigurationIndex As Long
    configuration As WD_USB_CONFIG_DESC
    dwInterfaceAlternatives As Long
    Interface(0 To WD_USB_MAX_INTERFACES - 1) As WD_USB_INTERFACE
End Type

Type WD_USB_HUB_GENERAL_INFO
    fBusPowered          As Long
    dwPorts              As Long  ' number of ports on this hub
    dwCharacteristics    As Long  ' Hub Charateristics
    dwPowerOnToPowerGood As Long  ' port power on till power good in 2ms
    dwHubControlCurrent  As Long  ' max current in mA
End Type

Type WD_USB_DEVICE_GENERAL_INFO
    deviceId            As WD_USB_ID
    dwHubNum            As Long
    dwPortNum           As Long
    fHub                As Long
    fFullSpeed          As Long
    dwConfigurationsNum As Long
    deviceAddress       As Long
    hubInfo             As WD_USB_HUB_GENERAL_INFO
End Type

Type WD_USB_DEVICE_INFO
    dwPipes As Long
    Pipe(0 To WD_USB_MAX_PIPE_NUMBER - 1) As WD_USB_PIPE_INFO
End Type

Type WD_USB_SCAN_DEVICES
    searchId  As WD_USB_ID ' if dwVendorId==0 - scan all vendor IDs
                           ' if dwProductId==0 - scan all product IDs
    dwDevices As Long
    uniqueId(0 To WD_USB_MAX_DEVICE_NUMBER - 1) As Long         ' a unique id to identify the device
    deviceGeneralInfo(0 To WD_USB_MAX_DEVICE_NUMBER - 1) As WD_USB_DEVICE_GENERAL_INFO
End Type


Global Const USB_TRANSFER_HALT = 1     ' USB TRANSFER options

Type WD_USB_TRANSFER
    hDevice      As Long       ' handle of USB device to read from or write to
    dwPipe       As Long       ' pipe number on device
    fRead        As Long
    dwOptions    As Long       ' USB_TRANSFER options:
                               '    USB_TRANSFER_HALT halts the pervious no wait transfer
    pBuffer      As Long       ' pointer to buffer to read/write
    dwBytes      As Long
    dwTimeout    As Long       ' timeout for the transfer in milli-seconds. 0==>no timeout.
    dwBytesTransfered As Long  ' returns the number of bytes actually read/written
    SetupPacket(0 To 8 - 1) As Byte     ' setup packet for control pipe transfer
    fOK          As Long       ' transfer succeeded
End Type

Type WD_USB_DEVICE_REGISTER
    uniqueId             As Long         ' the device unique ID
    dwConfigurationIndex As Long         ' the index of the configuration to register
    dwInterfaceNum       As Long         ' interface to register
    dwInterfaceAlternate As Long
    hDevice              As Long         ' handle of device
    Device               As WD_USB_DEVICE_INFO      ' description of the device
    dwOptions            As Long         ' should be zero
    cName(0 To 32 - 1)   As Byte         ' name of card
    cDescription(0 To 100 - 1) As Byte   ' description
End Type

Type WD_USB_RESET_PIPE
        hDevice As Long
        dwPipe As Long
End Type


Private IOCTL_WD_USB_RESET_PIPE         As Long
Private IOCTL_WD_USB_RESET_DEVICE       As Long
Private IOCTL_WD_USB_SCAN_DEVICES       As Long
Private IOCTL_WD_USB_SCAN_DEVICES_V432  As Long
Private IOCTL_WD_USB_TRANSFER           As Long
Private IOCTL_WD_USB_DEVICE_REGISTER    As Long
Private IOCTL_WD_USB_DEVICE_UNREGISTER  As Long
Private IOCTL_WD_USB_GET_CONFIGURATION  As Long
    
Dim WinDriverGlobalDW As Long

Global Const WD_TYPE As Long = 38200
Global Const FILE_ANY_ACCESS As Long = 0
Global Const METHOD_NEITHER As Long = 3

'    This is an implementation of a WinIOCTL macro (CTL_CODE)
Function Get_Ctl_Code(Nr As Integer) As Long
Dim lTMP As Long
Const lMaxLong As Long = 2147483647
   lTMP = (WD_TYPE * 2 ^ 15) Or (FILE_ANY_ACCESS * 2 ^ 13) + ((Nr * 4) Or (METHOD_NEITHER)) \ 2
   Get_Ctl_Code = 0 - (lMaxLong - lTMP) * 2 - 1
End Function ' Get_Ctl_Code

Function WD_Open() As Long
  Static WD_Initialized As Integer
  
  If (WD_Initialized = 0) Then
    WD_Initialize
    WD_Initialized = 1
  End If
  
  WD_Open = CreateFile("\\.\WINDRVR", GENERIC_READ, _
     FILE_SHARE_READ Or FILE_SHARE_WRITE, 0, _
     OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0)
End Function

Sub WD_Close(hWD As Long)
  CloseHandle hWD
End Sub

Sub WD_Debug(hWD As Long, Debug_parm As WD_DEBUG)
   DeviceIoControl hWD, IOCTL_WD_DEBUG, Debug_parm, _
                  LenB(Debug_parm), 0, 0, WinDriverGlobalDW, 0
End Sub ' WD_Debug

Sub WD_DebugDump(hWD As Long, DebugDump As WD_DEBUG_DUMP)
  DeviceIoControl hWD, IOCTL_WD_DEBUG_DUMP, DebugDump, _
                  LenB(DebugDump), 0, 0, WinDriverGlobalDW, 0
End Sub ' WD_DebugDump

Sub WD_Transfer(hWD As Long, Transfer As WD_TRANSFER)
  DeviceIoControl hWD, IOCTL_WD_TRANSFER, Transfer, _
                  LenB(Transfer), 0, 0, WinDriverGlobalDW, 0
End Sub ' WD_Transfer

Sub WD_MultiTransfer(hWD As Long, TransferArray As WD_TRANSFER, dwNumTransfers As Integer)
  DeviceIoControl hWD, IOCTL_WD_MULTI_TRANSFER, TransferArray, _
                  LenB(TransferArray) * dwNumTransfers, 0, _
                  0, WinDriverGlobalDW, 0
End Sub ' WD_MultiTransfer

Sub WD_DMALock(hWD As Long, Dma As WD_DMA)
  DeviceIoControl hWD, IOCTL_WD_DMA_LOCK, Dma, LenB(Dma), 0, 0, _
                  WinDriverGlobalDW, 0
End Sub ' WD_DMALock

Sub WD_DMAUnlock(hWD As Long, Dma As WD_DMA)
  DeviceIoControl hWD, IOCTL_WD_DMA_UNLOCK, Dma, LenB(Dma), 0, _
                  0, WinDriverGlobalDW, 0
End Sub ' WD_DMAUnlock

Sub WD_IntEnable(hWD As Long, TheInterrupt As WD_INTERRUPT)
  DeviceIoControl hWD, IOCTL_WD_INT_ENABLE, TheInterrupt, _
                  LenB(TheInterrupt), 0, 0, WinDriverGlobalDW, 0
End Sub ' WD_IntEnable

Sub WD_IntDisable(hWD As Long, TheInterrupt As WD_INTERRUPT)
  DeviceIoControl hWD, IOCTL_WD_INT_DISABLE, TheInterrupt, _
                  LenB(TheInterrupt), 0, 0, WinDriverGlobalDW, 0
End Sub ' WD_IntDisable

Sub WD_IntCount(hWD As Long, TheInterrupt As WD_INTERRUPT)
  DeviceIoControl hWD, IOCTL_WD_INT_COUNT, TheInterrupt, _
                  LenB(TheInterrupt), 0, 0, WinDriverGlobalDW, 0
End Sub ' WD_IntCount

Sub WD_IntWait(hWD As Long, TheInterrupt As WD_INTERRUPT)
Dim h As Long
  h = WD_Open()
  DeviceIoControl h, IOCTL_WD_INT_WAIT, TheInterrupt, _
                  LenB(TheInterrupt), 0, 0, WinDriverGlobalDW, 0
  WD_Close h
End Sub ' WD_IntWait

Sub WD_IsapnpScanCards(hWD As Long, IsapnpScanCards As WD_ISAPNP_SCAN_CARDS)
  DeviceIoControl hWD, IOCTL_WD_ISAPNP_SCAN_CARDS, IsapnpScanCards, _
                  LenB(IsapnpScanCards), 0, 0, WinDriverGlobalDW, 0
End Sub ' WD_IsapnpScanCards

Sub WD_IsapnpGetCardInfo(hWD As Long, IsapnpGetCardInfo As WD_ISAPNP_CARD_INFO)
  DeviceIoControl hWD, IOCTL_WD_ISAPNP_GET_CARD_INFO, IsapnpGetCardInfo, _
                  LenB(IsapnpGetCardInfo), 0, 0, WinDriverGlobalDW, 0
End Sub ' WD_IsapnpGetCardInfo

Sub WD_IsapnpConfigDump(hWD As Long, IsapnpConfigDump As WD_ISAPNP_CONFIG_DUMP)
  DeviceIoControl hWD, IOCTL_WD_ISAPNP_CONFIG_DUMP, IsapnpConfigDump, _
                  LenB(IsapnpConfigDump), 0, 0, WinDriverGlobalDW, 0
End Sub ' WD_IsapnpConfigDump

Sub WD_PcmciaScanCards(hWD As Long, PcmciaScanCards As WD_PCMCIA_SCAN_CARDS)
  DeviceIoControl hWD, IOCTL_WD_PCMCIA_SCAN_CARDS, PcmciaScanCards, _
                                  LenB(PcmciaScanCards), 0, 0, WinDriverGlobalDW, 0
End Sub ' WD_PcmciaScanCards

Sub WD_PcmciaGetCardInfo(hWD As Long, PcmciaCardInfo As WD_PCMCIA_CARD_INFO)
  DeviceIoControl hWD, IOCTL_WD_PCMCIA_GET_CARD_INFO, PcmciaCardInfo, _
                                  LenB(PcmciaCardInfo), 0, 0, WinDriverGlobalDW, 0
End Sub ' WD_PcmciaGetCardInfo

Sub WD_PcmciaConfigDump(hWD As Long, PcmciaConfigDump As WD_PCMCIA_CONFIG_DUMP)
  DeviceIoControl hWD, IOCTL_WD_PCMCIA_CONFIG_DUMP, PcmciaConfigDump, _
                          LenB(PcmciaConfigDump), 0, 0, WinDriverGlobalDW, 0
End Sub ' WD_PcmciaConfigDump

Sub WD_Sleep(hWD As Long, Sleep As WD_SLEEP)
  DeviceIoControl hWD, IOCTL_WD_SLEEP, Sleep, _
                  LenB(Sleep), 0, 0, WinDriverGlobalDW, 0
End Sub ' WD_Sleep

Sub WD_CardRegister(hWD As Long, Card As WD_CARD_REGISTER)
   DeviceIoControl hWD, IOCTL_WD_CARD_REGISTER, Card, _
                  LenB(Card), 0, 0, WinDriverGlobalDW, 0
End Sub ' WD_CardRegister

Sub WD_CardUnregister(hWD As Long, Card As WD_CARD_REGISTER)
  DeviceIoControl hWD, IOCTL_WD_CARD_UNREGISTER, Card, _
                  LenB(Card), 0, 0, WinDriverGlobalDW, 0
End Sub ' WD_CardUnregister

Sub WD_PciScanCards(hWD As Long, PciScan As WD_PCI_SCAN_CARDS)
   DeviceIoControl hWD, IOCTL_WD_PCI_SCAN_CARDS, PciScan, _
                  LenB(PciScan), 0, 0, WinDriverGlobalDW, 0
End Sub ' WD_PciScanCards

Sub WD_PciGetCardInfo(hWD As Long, PciCard As WD_PCI_CARD_INFO)
 DeviceIoControl hWD, IOCTL_WD_PCI_GET_CARD_INFO, PciCard, _
                  LenB(PciCard), 0, 0, WinDriverGlobalDW, 0
End Sub ' WD_PciGetCardInfo

Sub WD_Version(hWD As Long, VerInfo As WD_VERSION)
  DeviceIoControl hWD, IOCTL_WD_VERSION, VerInfo, _
                  LenB(VerInfo), 0, 0, WinDriverGlobalDW, 0
End Sub ' WD_Version

Sub WD_License(hWD As Long, License As WD_LICENSE)
  DeviceIoControl hWD, IOCTL_WD_LICENSE, License, _
                  LenB(License), 0, 0, WinDriverGlobalDW, 0
End Sub ' WD_License

Sub WD_KernelPlugInOpen(hWD As Long, KernelPlugInOpen As WD_KERNEL_PLUGIN)
  DeviceIoControl hWD, IOCTL_WD_KERNEL_PLUGIN_OPEN, KernelPlugInOpen, _
                  LenB(KernelPlugInOpen), 0, 0, WinDriverGlobalDW, 0
End Sub ' WD_KernelPlugInOpen

Sub WD_KernelPlugInClose(hWD As Long, KernelPlugInClose As WD_KERNEL_PLUGIN)
  DeviceIoControl hWD, IOCTL_WD_KERNEL_PLUGIN_CLOSE, KernelPlugInClose, _
                  LenB(KernelPlugInClose), 0, 0, WinDriverGlobalDW, 0
End Sub ' WD_KernelPlugInClose

Sub WD_KernelPlugInCall(hWD As Long, KernelPlugInCall As WD_KERNEL_PLUGIN_CALL)
  DeviceIoControl hWD, IOCTL_WD_KERNEL_PLUGIN_CALL, KernelPlugInCall, _
                  LenB(KernelPlugInCall), 0, 0, WinDriverGlobalDW, 0
End Sub ' WD_KernelPlugInCall

Sub WD_PciConfigDump(hWD As Long, PciConfigDump As WD_PCI_CONFIG_DUMP)
  DeviceIoControl hWD, IOCTL_WD_PCI_CONFIG_DUMP, PciConfigDump, _
                  LenB(PciConfigDump), 0, 0, WinDriverGlobalDW, 0
End Sub ' WD_PciConfigDump

Sub WD_UsbScanDevice(hWD As Long, scan As WD_USB_SCAN_DEVICES)
  DeviceIoControl hWD, IOCTL_WD_USB_SCAN_DEVICES, scan, _
                  LenB(scan), 0, 0, WinDriverGlobalDW, 0
End Sub ' WD_UsbScanDevice

Sub WD_UsbGetConfiguration(hWD As Long, Config As WD_USB_CONFIGURATION)
  DeviceIoControl hWD, IOCTL_WD_USB_GET_CONFIGURATION, Config, _
                  LenB(Config), 0, 0, WinDriverGlobalDW, 0
End Sub ' WD_UsbGetConfiguration

Sub WD_UsbDeviceRegister(hWD As Long, dev As WD_USB_DEVICE_REGISTER)
  DeviceIoControl hWD, IOCTL_WD_USB_DEVICE_REGISTER, dev, _
                  LenB(dev), 0, 0, WinDriverGlobalDW, 0
End Sub ' WD_UsbDeviceRegister

Sub WD_UsbTransfer(hWD As Long, trans As WD_USB_TRANSFER)
Dim h As Long
  h = WD_Open()
  DeviceIoControl h, IOCTL_WD_USB_TRANSFER, trans, _
                  LenB(trans), 0, 0, WinDriverGlobalDW, 0
  WD_Close h
End Sub ' WD_UsbTransfer

Sub WD_UsbDeviceUnregister(hWD As Long, dev As WD_USB_DEVICE_REGISTER)
  DeviceIoControl hWD, IOCTL_WD_USB_DEVICE_UNREGISTER, dev, _
                  LenB(dev), 0, 0, WinDriverGlobalDW, 0
End Sub ' WD_UsbDeviceUnregister

Sub WD_UsbResetPipe(hWD As Long, ResetPipe As WD_USB_RESET_PIPE)
  DeviceIoControl hWD, IOCTL_WD_USB_RESET_PIPE, ResetPipe, _
                  LenB(ResetPipe), 0, 0, WinDriverGlobalDW, 0
End Sub ' WD_UsbResetPipe

Sub WD_UsbResetDevice(hWD As Long, hDev As Long)
  DeviceIoControl hWD, IOCTL_WD_USB_RESET_DEVICE, hDev, _
                  LenB(hDev), 0, 0, WinDriverGlobalDW, 0
End Sub ' WD_UsbResetDevice

Sub WD_Initialize()

    IOCTL_WD_DMA_LOCK                 = Get_Ctl_Code(&H901)
    IOCTL_WD_DMA_UNLOCK               = Get_Ctl_Code(&H902)
    IOCTL_WD_TRANSFER                 = Get_Ctl_Code(&H903)
    IOCTL_WD_MULTI_TRANSFER           = Get_Ctl_Code(&H904)
    IOCTL_WD_PCI_SCAN_CARDS           = Get_Ctl_Code(&H90E)
    IOCTL_WD_PCI_GET_CARD_INFO        = Get_Ctl_Code(&H90F)
    IOCTL_WD_VERSION                  = Get_Ctl_Code(&H910)
    IOCTL_WD_PCI_CONFIG_DUMP          = Get_Ctl_Code(&H91A)
    IOCTL_WD_KERNEL_PLUGIN_OPEN       = Get_Ctl_Code(&H91B)
    IOCTL_WD_KERNEL_PLUGIN_CLOSE      = Get_Ctl_Code(&H91C)
    IOCTL_WD_KERNEL_PLUGIN_CALL       = Get_Ctl_Code(&H91D)
    IOCTL_WD_INT_ENABLE               = Get_Ctl_Code(&H91E)
    IOCTL_WD_INT_DISABLE              = Get_Ctl_Code(&H91F)
    IOCTL_WD_INT_COUNT                = Get_Ctl_Code(&H920)
    IOCTL_WD_ISAPNP_SCAN_CARDS        = Get_Ctl_Code(&H924)
    IOCTL_WD_ISAPNP_CONFIG_DUMP       = Get_Ctl_Code(&H926)
    IOCTL_WD_SLEEP                    = Get_Ctl_Code(&H927)
    IOCTL_WD_DEBUG                    = Get_Ctl_Code(&H928)
    IOCTL_WD_DEBUG_DUMP               = Get_Ctl_Code(&H929)
    IOCTL_WD_CARD_UNREGISTER          = Get_Ctl_Code(&H92B)
    IOCTL_WD_ISAPNP_GET_CARD_INFO     = Get_Ctl_Code(&H92D)
    IOCTL_WD_PCMCIA_SCAN_CARDS        = Get_Ctl_Code(&H92F)
    IOCTL_WD_PCMCIA_GET_CARD_INFO     = Get_Ctl_Code(&H930)
    IOCTL_WD_PCMCIA_CONFIG_DUMP       = Get_Ctl_Code(&H931)
    IOCTL_WD_CARD_REGISTER            = Get_Ctl_Code(&H956)
    IOCTL_WD_INT_WAIT                 = Get_Ctl_Code(&H94B)
    IOCTL_WD_LICENSE                  = Get_Ctl_Code(&H952)
    IOCTL_WD_USB_RESET_PIPE           = Get_Ctl_Code(&H93C)
    IOCTL_WD_USB_RESET_DEVICE         = Get_Ctl_Code(&H93F)
    IOCTL_WD_USB_SCAN_DEVICES_V432    = Get_Ctl_Code(&H940)
    IOCTL_WD_USB_SCAN_DEVICES         = Get_Ctl_Code(&H95A)
    IOCTL_WD_USB_TRANSFER             = Get_Ctl_Code(&H942)
    IOCTL_WD_USB_DEVICE_REGISTER      = Get_Ctl_Code(&H949)
    IOCTL_WD_USB_DEVICE_UNREGISTER    = Get_Ctl_Code(&H959)
    IOCTL_WD_USB_GET_CONFIGURATION    = Get_Ctl_Code(&H946)


End Sub
